package com.phkj.qdh.ui.fragment.user

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.phkj.qdh.base.BaseVM
import com.phkj.qdh.bean.MyBaseInspectBean
import com.phkj.qdh.bean.MyBaseInspectDetailBean
import com.phkj.qdh.bean.MyFailoverDetailBean
import com.phkj.qdh.bean.MyFailoverListBean
import com.phkj.qdh.bean.MyLawDetail
import com.phkj.qdh.bean.MyTeamDetail
import com.phkj.qdh.bean.ThreeScoreBean
import com.phkj.qdh.bean.ThreeScoreRecordBean
import com.phkj.qdh.bean.UserCenterBean
import com.phkj.qdh.bean.UserMyNoticeTeamOrLawBean
import com.phkj.qdh.net.code
import com.phkj.qdh.net.msg
import com.phkj.qdh.repository.UserRepository
import com.phkj.qdh.utils.LogUtil
import com.phkj.qdh.utils.requestIO
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onCompletion

class UserVM(private val repository: UserRepository) : BaseVM() {
    private val _state = MutableSharedFlow<Int>()
    val state get() = _state

    private suspend fun stop(i: Int) {
        _state.emit(i)
    }

    private val _threeScore = MutableSharedFlow<ThreeScoreBean>()
    val threeScore get() = _threeScore

    fun getThreeScore() {
        requestIO {
            repository.getThreeScore().catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _threeScore.emit(it)
                stop(0)
            }
        }
    }


    private val _threeScoreList = MutableSharedFlow<ArrayList<ThreeScoreRecordBean>>()
    val threeScoreList get() = _threeScoreList

    fun getThreeScoreList() {
        requestIO {
            repository.getThreeScoreList().catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _threeScoreList.emit(it)
                stop(0)
            }
        }
    }


    private val _userCenter = MutableSharedFlow<UserCenterBean>()
    val userCenter get() = _userCenter

    fun getUserCenter() {
        requestIO {
            repository.getUserCenter().catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _userCenter.emit(it)
            }
        }
    }


    //队伍通告
    private val _baseNotice = MutableSharedFlow<ArrayList<MyBaseInspectBean>>()
    val baseNotice get() = _baseNotice

    fun getBaseNotice(map: Map<String, Any?>) {
        requestIO {
            repository.getBaseNotice(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _baseNotice.emit(it.data)
            }
        }
    }

    private val _baseDetail = MutableSharedFlow<MyBaseInspectDetailBean>()
    val baseDetail get() = _baseDetail

    fun getBaseDetail(id: Int) {
        requestIO {
            repository.getBaseDetail(id).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _baseDetail.emit(it)
            }
        }
    }

    private val _baseEditResult = MutableSharedFlow<String>()
    val baseEditResult get() = _baseEditResult

    fun editBaseDetail(tip: String, map: Map<String, Any?>) {
        requestIO {
            repository.editBaseDetail(map).catch { cause ->
                if (cause.code == 0) {
                    ToastUtils.show(tip)
                    _baseEditResult.emit(tip)
                }else{
                    ToastUtils.show(cause.msg)
                }
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {

            }
        }
    }


    //队伍通告
    private val _teamNotice = MutableSharedFlow<ArrayList<UserMyNoticeTeamOrLawBean>>()
    val teamNotice get() = _teamNotice

    fun getTeamNotice(map: Map<String, Any?>) {
        requestIO {
            repository.getTeamOrLawNotice(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
                LogUtil.E("加载完成1")
            }.collectLatest {
                _teamNotice.emit(it.data)
            }
        }
    }

    private val _teamDetail = MutableSharedFlow<MyTeamDetail>()
    val teamDetail get() = _teamDetail

    fun getTeamDetail(id: Int) {
        requestIO {
            repository.getTeamDetail(id).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _teamDetail.emit(it)
            }
        }
    }

    private val _teamRefuse = MutableSharedFlow<Boolean>()
    val teamRefuse get() = _teamRefuse

    fun teamRefuse(map: Map<String, Any?>) {
        requestIO {
            repository.teamRefuse(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _teamRefuse.emit(it)
            }
        }
    }

    private val _lawRefuse = MutableSharedFlow<Boolean>()
    val lawRefuse get() = _lawRefuse

    fun lawRefuse(map: Map<String, Any?>) {
        requestIO {
            repository.lawRefuse(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _lawRefuse.emit(it)
            }
        }
    }


    //执法通告
    private val _lawNotice = MutableSharedFlow<ArrayList<UserMyNoticeTeamOrLawBean>>()
    val lawNotice get() = _lawNotice

    fun getLawNotice(map: Map<String, Any?>) {
        requestIO {
            repository.getTeamOrLawNotice(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
                LogUtil.E("加载完成1")
            }.collectLatest {
                _lawNotice.emit(it.data)
            }
        }
    }

    private val _lawDetail = MutableSharedFlow<MyLawDetail>()
    val lawDetail get() = _lawDetail

    fun getLawDetail(id: Int) {
        requestIO {
            repository.getLawDetail(id).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
                LogUtil.E("加载完成1")
            }.collectLatest {
                _lawDetail.emit(it)
            }
        }
    }


    private val _myFailoverList = MutableSharedFlow<ArrayList<MyFailoverListBean>>()
    val myFailoverList get() = _myFailoverList
    fun getMyFailoverList(map: Map<String, Any?>) {
        requestIO {
            repository.getMyFailoverList(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
                LogUtil.E("加载完成")
            }.collectLatest {
                _myFailoverList.emit(it.data)
            }
        }
    }


    private val _myFailoverDetail = MutableSharedFlow<MyFailoverDetailBean>()
    val myFailoverDetail get() = _myFailoverDetail
    fun getMyFailoverDetail(id: Int) {
        requestIO {
            repository.getMyFailoverDetail(id).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
                LogUtil.E("加载完成")
            }.collectLatest {
                _myFailoverDetail.emit(it)
            }
        }
    }

    private val _teamFeedback = MutableSharedFlow<Boolean>()
    val teamFeedback get() = _teamFeedback
    fun teamFeedback(map: Map<String, Any?>) {
        requestIO {
            repository.teamFeedback(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _teamFeedback.emit(it)
            }
        }
    }


    private val _lawFeedback = MutableSharedFlow<Boolean>()
    val lawFeedback get() = _lawFeedback
    fun lawFeedback(map: Map<String, Any?>) {
        requestIO {
            repository.lawFeedback(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(cause.code)
            }.onCompletion {
                stop(0)
            }.collectLatest {
                _lawFeedback.emit(it)
            }
        }
    }


}

fun getUserVMProvider() = UserVMProvider(UserRepository)

class UserVMProvider(private val repository: UserRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return UserVM(repository) as T
    }
}
